package com.example.ivory.accountbook.uilt;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.ivory.accountbook.Activity.AddItem;
import com.example.ivory.accountbook.R;

import java.util.List;
import java.util.Map;

/**
 * Created by lixin on 16/8/26.
 */
public class MyAdspter extends BaseAdapter{
    private List<Map<String,Object>> data;
    private LayoutInflater layoutInflater;
    private Context context;

    public MyAdspter(Context context,List<Map<String,Object>> data){
        this.context=context;
        this.data=data;
        this.layoutInflater=LayoutInflater.from(context);
    }
    public class WidgetSet{
        public Button leftbtn;
        public TextView lefttext;
        public ImageView image;
        public TextView righttext;
        public Button rightbtn;
    }
    @Override
    public int getCount(){
        return data!=null?data.size():0;
    }
    @Override
    public Object getItem(int position) {
        return data.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        WidgetSet widgetSet = null;
        if (convertView == null) {
            widgetSet = new WidgetSet();
            convertView = layoutInflater.inflate(R.layout.activity_item, null);
            widgetSet.leftbtn=(Button)convertView.findViewById(R.id.leftbtn) ;
            widgetSet.lefttext=(TextView)convertView.findViewById(R.id.lefttext);
            widgetSet.image=(ImageView) convertView.findViewById(R.id.image);
            widgetSet.righttext=(TextView)convertView.findViewById(R.id.righttext);
            widgetSet.rightbtn=(Button)convertView.findViewById(R.id.rightbtn);
            convertView.setTag(widgetSet);
        }else{
            widgetSet=(WidgetSet)convertView.getTag();
        }
        widgetSet.lefttext.setText((String)data.get(position).get("left"));
        widgetSet.righttext.setText((String)data.get(position).get("right"));
        String image = (String) data.get(position).get("image");
        int drawable = context.getResources().getIdentifier(image, "drawable", context.getPackageName());
        widgetSet.image.setImageResource(drawable);
        return convertView;
    }
}
